package com.innovation.sys.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.innovation.sys.dao.SSxsArticleMapper;
import com.innovation.sys.pojo.SSxsArticle;
import com.innovation.sys.service.ArticleService;

@Service
public class ArticleServiceImpl implements ArticleService {

	@Resource
	private SSxsArticleMapper articleMapper;

	@Override
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public int add(SSxsArticle article) {
		Date now = new Date();
		article.setCreateTime(now);
		article.setStatus(2);
		article.setDelFlag(0);
		article.setIsHot(1);
		article.setPubTime(now);
		article.setType(1);
		return articleMapper.insert(article);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public PageInfo<Map<String, Object>> selectByOption(Map map, Integer pageNum, Integer pageSize) {
		int pageNo = pageNum == null ? 1 : pageNum;
		int pageSiz = pageSize == null ? 10 : pageSize;
		PageHelper.startPage(pageNo, pageSiz);
		List<Map<String, Object>> pList = articleMapper.findAll(map);
		PageInfo<Map<String, Object>> page = new PageInfo<Map<String, Object>>(pList);
		return page;
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public int deleteOne(Integer articleId) {
		SSxsArticle article = new SSxsArticle();
		article.setDelFlag(1);
		article.setArticleId(articleId);
		article.setStatus(3);
		return articleMapper.updateArticle(article);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public SSxsArticle findOne(Integer articleId) {
		SSxsArticle sSxsArticle = new SSxsArticle();
		sSxsArticle.setArticleId(articleId);
		return articleMapper.selectOne(sSxsArticle);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public int updateOne(SSxsArticle article) {
		article.setUpdateTime(new Date());
		return articleMapper.updateArticle(article);
	}

}
